from PyQt4 import QtGui

from fogmini.login import login
from fogmini.utils import get_config_value, set_config_value

class LoginDetails(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        layout = QtGui.QFormLayout()

        self.host_name = QtGui.QLineEdit(get_config_value("base_url", "http://example.fogbugz.com").toString(), self)
        layout.addRow("Host Name", self.host_name)

        self.username = QtGui.QLineEdit(get_config_value("username", "").toString(), self)
        layout.addRow("Username", self.username)

        self.password = QtGui.QLineEdit(get_config_value("password", "").toString(), self)
        layout.addRow("Password", self.password)

        self.button_box = QtGui.QDialogButtonBox()
        self.button_box.addButton("Log In", 0)
        self.button_box.addButton("Cancel", 1)
        layout.addRow(self.button_box)

        self.button_box.accepted.connect(self.accept)
        self.button_box.rejected.connect(self.reject)

        self.setLayout(layout)

        self.resize(300, 100)

    def accept(self):
        set_config_value("base_url", self.host_name.text())
        set_config_value("username", self.username.text())
        set_config_value("password", self.password.text())

        login()

        QtGui.QDialog.accept()
